// 功能测试工具
import { api } from '../api/request'
import { ElMessage } from 'element-plus'

export class FunctionTester {
  constructor() {
    this.testResults = []
  }

  // 测试健康分析功能
  async testHealthAnalysis() {
    try {
      console.log('🔍 测试健康分析功能...')
      
      // 测试获取健康分析
      const response = await api.get('/api/health/analysis')
      
      if (response.success) {
        this.addResult('健康分析', '✅ 通过', '成功获取健康分析数据')
        return true
      } else {
        this.addResult('健康分析', '❌ 失败', response.message || '获取数据失败')
        return false
      }
    } catch (error) {
      this.addResult('健康分析', '❌ 错误', error.message || '接口调用异常')
      return false
    }
  }

  // 测试预约功能
  async testAppointments() {
    try {
      console.log('🔍 测试预约功能...')
      
      // 测试获取预约列表
      const response = await api.get('/api/appointments/my')
      
      if (response.success) {
        this.addResult('预约管理', '✅ 通过', '成功获取预约列表')
        return true
      } else {
        this.addResult('预约管理', '❌ 失败', response.message || '获取预约失败')
        return false
      }
    } catch (error) {
      this.addResult('预约管理', '❌ 错误', error.message || '接口调用异常')
      return false
    }
  }

  // 测试通知功能
  async testNotifications() {
    try {
      console.log('🔍 测试通知功能...')
      
      // 测试获取通知列表
      const response = await api.get('/api/notifications')
      
      if (response.success) {
        this.addResult('通知中心', '✅ 通过', '成功获取通知列表')
        return true
      } else {
        this.addResult('通知中心', '❌ 失败', response.message || '获取通知失败')
        return false
      }
    } catch (error) {
      this.addResult('通知中心', '❌ 错误', error.message || '接口调用异常')
      return false
    }
  }

  // 测试健康记录功能
  async testHealthRecord() {
    try {
      console.log('🔍 测试健康记录功能...')
      
      // 测试获取健康记录
      const response = await api.get('/api/health/record')
      
      if (response.success) {
        this.addResult('健康记录', '✅ 通过', '成功获取健康记录')
        return true
      } else {
        this.addResult('健康记录', '❌ 失败', response.message || '获取记录失败')
        return false
      }
    } catch (error) {
      this.addResult('健康记录', '❌ 错误', error.message || '接口调用异常')
      return false
    }
  }

  // 测试症状评估功能
  async testSymptomAssessment() {
    try {
      console.log('🔍 测试症状评估功能...')
      
      // 测试症状评估
      const testData = {
        symptoms: ['头痛', '发热'],
        duration: '2天',
        severity: '中等'
      }
      
      const response = await api.post('/api/symptoms/assess', testData)
      
      if (response.success) {
        this.addResult('症状评估', '✅ 通过', '成功进行症状评估')
        return true
      } else {
        this.addResult('症状评估', '❌ 失败', response.message || '评估失败')
        return false
      }
    } catch (error) {
      this.addResult('症状评估', '❌ 错误', error.message || '接口调用异常')
      return false
    }
  }

  // 测试AI聊天功能
  async testAIChat() {
    try {
      console.log('🔍 测试AI聊天功能...')
      
      // 测试AI聊天
      const testMessage = {
        message: '你好，我想咨询一下健康问题',
        type: 'text'
      }
      
      const response = await api.post('/api/chat', testMessage)
      
      if (response.success) {
        this.addResult('AI聊天', '✅ 通过', '成功进行AI对话')
        return true
      } else {
        this.addResult('AI聊天', '❌ 失败', response.message || '聊天失败')
        return false
      }
    } catch (error) {
      this.addResult('AI聊天', '❌ 错误', error.message || '接口调用异常')
      return false
    }
  }

  // 添加测试结果
  addResult(feature, status, message) {
    this.testResults.push({
      feature,
      status,
      message,
      timestamp: new Date().toLocaleTimeString()
    })
  }

  // 运行所有测试
  async runAllTests() {
    console.log('🚀 开始运行功能测试...')
    this.testResults = []
    
    const tests = [
      this.testHealthAnalysis(),
      this.testAppointments(),
      this.testNotifications(),
      this.testHealthRecord(),
      this.testSymptomAssessment(),
      this.testAIChat()
    ]
    
    await Promise.all(tests)
    
    // 统计测试结果
    const passed = this.testResults.filter(r => r.status.includes('✅')).length
    const failed = this.testResults.filter(r => r.status.includes('❌')).length
    
    console.log(`📊 测试完成: ${passed} 个通过, ${failed} 个失败`)
    
    if (failed === 0) {
      ElMessage.success(`🎉 所有功能测试通过！(${passed}/${this.testResults.length})`)
    } else {
      ElMessage.warning(`⚠️ 部分功能测试失败: ${passed}/${this.testResults.length} 通过`)
    }
    
    return this.testResults
  }

  // 获取测试结果
  getResults() {
    return this.testResults
  }

  // 清除测试结果
  clearResults() {
    this.testResults = []
  }
}

// 导出单例实例
export const functionTester = new FunctionTester()